package com.protocols.sctp.m3ua.sccp.tcap.camel;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.HashMap;
import java.util.logging.Logger;

import com.protocols.GeneralProtocolData;
import com.protocols.sctp.m3ua.sccp.tcap.TcapHandlerInterface;

public class SubscriberHandler extends java.rmi.server.UnicastRemoteObject implements SubscriberHandlerInterface{
	
	public int Balance=34;
	TcapHandlerInterface TcapHandler=null;
	public int InvokeID=80;
	private Boolean callconnected=false;
	private Boolean callinLive=true;
	String OGT="";
	String DGT="";
	long OrigTransactionID=0;
	long DestTransactionID=0;
	String CallingParty="";
	String CalledParty ="";
	
	public SubscriberHandler() throws RemoteException  {	
		super();
		try{
			
			if (TcapHandler==null){
				Registry registry=LocateRegistry.getRegistry(9001);	
				TcapHandler=(TcapHandlerInterface)(registry.lookup("TcapHandler"));
			}
			
			while(callinLive){
				if (callconnected){
					Balance--;
					if (Balance<=0){
						// Send Release
					}
					Thread.sleep(500);
				}
				
				Thread.sleep(500);
			}
			
		}catch(Exception e){
			
			
		}
	
	}


	@Override
	public void initialDP(long sessionid,  int OPC, int DPC, String OGT, String DGT, long OrigTransactionID, long DestTransactionID,String callingParty, String calledParty ) throws RemoteException {
		this.OGT=OGT;
		this.DGT=DGT;
		this.OrigTransactionID=OrigTransactionID;
		this.DestTransactionID=DestTransactionID;
		this.CallingParty=callingParty;
		this.CalledParty =calledParty;
		
		if (Balance>0){
			CamelRequestReportBCSMEvent requestreport = new CamelRequestReportBCSMEvent();
			requestreport.setInvokeID(++InvokeID);
			requestreport.BCSMEvent1.setBCSMEvent(5, 1);
			requestreport.BCSMEvent1.setLegId(true, 2);
			requestreport.BCSMEvent2.setBCSMEvent(7, 1);
			requestreport.BCSMEvent2.setLegId(true, 2);
			requestreport.BCSMEvent3.setBCSMEvent(9, 0);
			requestreport.BCSMEvent3.setLegId(true, 1);
			requestreport.BCSMEvent4.setBCSMEvent(9, 0);
			requestreport.BCSMEvent4.setLegId(true, 2);
			requestreport.BCSMEvent5.setBCSMEvent(10, 1);
			requestreport.BCSMEvent5.setLegId(true, 1);
			requestreport.BCSMEvent5.setBCSMEvent(4, 1);
			requestreport.BCSMEvent5.setLegId(true, 2);
			
			
			TcapHandler.camelToTcap(sessionid, requestreport, DGT, OGT, DestTransactionID, OrigTransactionID);
			
			//Camel Continue
			CamelContinue camelcontinue = new CamelContinue();
			camelcontinue.setInvokeID(++InvokeID);
			
			TcapHandler.camelToTcap(sessionid, camelcontinue, DGT, OGT, DestTransactionID, OrigTransactionID);
			
			// Camel 
			CamelConnect camelconnect = new CamelConnect();
			camelconnect.setInvokeID(82);
			camelconnect.setDestinationNumber(0x8310, "995");
			
			//TcapHandler.camelToTcap(sessionid, camelconnect, DGT, OGT, DestTransactionID, OrigTransactionID);
			
		}
		else
		{
			CamelReleaseCall release = new CamelReleaseCall();
			release.setInvokeID(81);
			release.setCause(0, 0, 31);
			TcapHandler.camelToTcap(sessionid, release, DGT, OGT, DestTransactionID, OrigTransactionID);
			// Send a release
		}
		
	}


	@Override
	public void addBCSMEvents(long sessionid,CamelEventReportBCSM bcsmEvents) throws RemoteException {
		// TODO Auto-generated method stub
		switch((int)bcsmEvents.getEventTypeBCSM()){
		case 7:{	// oAnswered
			callconnected=true;
			break;
		}
		case 9:{	// oDisconnect
			callinLive=false;
			CamelReleaseCall release = new CamelReleaseCall();
			release.setInvokeID(81);
			release.setCause(0, 0, 31);
			TcapHandler.camelToTcap(sessionid, release, DGT, OGT, DestTransactionID, OrigTransactionID);
			
			break;
		}
		
		}
		
	}
	
	
	
	

}
